﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.HttpHandlers;
using System.Web.Routing;
using MvcEngine.Core.Repositories;

using MvcEngine.Core;
using System.IO;
using System.Web;
using MvcEngine.Core.Extensions;
using MvcEngine.Core.Content;

namespace MvcEngine.Blog.Components.Services
{
    public class HtmlTextFileResolver : IFileResolver
    {
        #region IFileResolver Members

        public string GetFilePath(RouteData routeData)
        {
            var pageContentStr = (string)routeData.Values["ctrId"];

            if (!string.IsNullOrEmpty(pageContentStr))
            {
                int ctrId;
                if (int.TryParse(pageContentStr, out ctrId))
                {
                    Control control = ContentManager.Current.GetControlByKey(routeData.GetRequiredString("controlKey"));
                    Module module = ContentManager.Current.GetModule(control.ModuleId);
                    //BlogPosts
                    string filePath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "Modules") + Path.DirectorySeparatorChar +
                        module.Name + Path.DirectorySeparatorChar + "Content" +
                        Path.DirectorySeparatorChar + ctrId.ToString() + Path.DirectorySeparatorChar
                        + routeData.GetRequiredString("fileName").UrlDecode();

                    return filePath;
                }
            }
            return "not_found.jpeg";
        }

        #endregion
    }
}
